<?php
namespace app\admin\controller;
use app\admin\logic\ClassKuLogic;
use think\Db;

/**
 * 题库api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/22
 * Time: 15:08
 */
class ClassKu extends Base {

    public function __construct(){
        parent::__construct();
        $this->ck_logic = new ClassKuLogic();
    }

    /**
     * 获取题库
     * @return \think\response\Json
     */
    public function ajax_get_ck(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->ck_logic->get_search_ck_maps($params['name']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('class_ku')->get_list($maps,'id,name,remark,c_time','id desc','',$limit,$params['rows']);
        $total_num = model('class_ku')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->ck_logic->get_response(1,'OK',$data));
    }

    /**
     * 新增题库
     * @return \think\response\Json
     */
    public function ajax_add_ck(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查新增题库规则
        $r = $this->ck_logic->check_add_ck_rule($params);
        if(!$r['code']){
            return json($r);
        }

        Db::startTrans();
        //新增题库
        $r = $this->ck_logic->add_ck($params);
        if(!$r['code']){
            Db::rollback();
            return json($r);
        }
        $ckid = $r['data']['ckid'];

        $file = request()->file('ti_file');
        $r = $this->ck_logic->import_ti($ckid,$file);
        if(!$r['code']){
            Db::rollback();
            return json($r);
        }
        $total_num = $r['data']['total_num'];
        $success_num = $r['data']['success_num'];
        Db::commit();
        return json($this->ck_logic->get_response(1,'导入成功，总数量：' . $total_num . '，成功数量：' . $success_num));
    }

    /**
     * 修改题库
     * @return \think\response\Json
     */
    public function ajax_save_ck(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查修改题库规则
        $r = $this->ck_logic->check_save_ck_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ck_detail = $r['data']['ck_detail'];

        $ck_data = [];
        $ck_data['name'] = trim($params['name']);
        $ck_data['remark'] = trim($params['remark']);

        $maps = [];
        $maps[] = ['id','=',$ck_detail['id']];
        $r = model('class_ku')->force()->save($ck_data,$maps);
        if($r !== false) {
            return json($this->ck_logic->get_response(1, '修改成功'));
        }
        return json($this->ck_logic->get_response(0, '修改失败'));
    }

    /**
     * 删除题库
     * @return \think\response\Json
     */
    public function ajax_del_ck(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查删除题库规则
        $r = $this->ck_logic->check_del_ck_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $r = model('class_ku')->destroy($params['ckid']);
        if($r){
            return json($this->ck_logic->get_response(1,'删除成功'));
        }
        return json($this->ck_logic->get_response(0,'删除失败'));
    }

    /**
     * 获取公用题库
     * @return \think\response\Json
     */
    public function ajax_get_public_ck(){
        $params = input();
        $r = $this->check_admin_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        $maps = $this->ck_logic->get_search_public_ck_maps();
        $list = model('class_ku')->get_list($maps,'id,name');

        $data['list'] = $list;
        return json($this->ck_logic->get_response(1,'OK',$data));
    }
}